package com.star.service.impl;

import com.star.NotFoundException;
import com.star.dao.BlogMapper;
import com.star.pojo.Blog;
import com.star.service.BlogService;
import com.star.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * com.star.service.impl
 *
 * @Description: 博客实现类
 * @Date: 2020-08-07 15:18
 * @Author: LENSTAR
 * @URL: http://123.57.161.114:9000/
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    //查询
    @Override
    public List<BlogVo> getAllBlog() {
        return blogMapper.getAllBlog();
    }

    //新增
    @Transactional
    @Override
    public int saveBlog(Blog blog) {
        // 在新增博客中需要初始化创建时间、更新时间、浏览数量、评论数量
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blog.setViews(0);
        blog.setCommentCount(0);
        return blogMapper.saveBlog(blog);
    }

    //修改
    @Transactional
    @Override
    public int updateBlog(ShowBlog showBlog) {
        showBlog.setUpdateTime(new Date());
        return blogMapper.updateBlog(showBlog);
    }

    //查询编辑的博客
    @Override
    public ShowBlog getShowBlogById(Long id) {
        ShowBlog showBlog = blogMapper.getShowBlogById(id);
        if (showBlog == null) {
            throw new NotFoundException("该博客不存在！");
        }
        return showBlog;
    }

    //删除
    @Transactional
    @Override
    public int deleteBlog(Long id) {
        return blogMapper.deleteBlog(id);
    }

    @Override
    public List<BlogVo> searchByTitleAndType(SearchBlog searchBlog) {
        List<BlogVo> blogVos = blogMapper.searchByTitleAndType(searchBlog);
        return blogVos;
    }

    @Override
    public List<FirstPageBlog> getFirstPageBlog() {
        List<FirstPageBlog> firstPageBlog = blogMapper.getFirstPageBlog();
        return firstPageBlog;
    }

    @Override
    public List<FirstPageBlog> searchFirstBlogByTitle(String title) {
        List<FirstPageBlog> firstPageBlogs = blogMapper.searchFirstBlogByTitle(title);
        return firstPageBlogs;
    }

    @Override
    public DetailedBlog getDetailedBlog(Long id) {
        DetailedBlog detailedBlog = blogMapper.getDetailedBlog(id);
        return detailedBlog;
    }

    @Override
    public int updateViews(Long id) {
        return blogMapper.updateViews(id);
    }

    @Override
    public int getCommentCountById(Long id) {
        return blogMapper.getCommentCountById(id);
    }

    @Override
    public List<FirstPageBlog> getByTypeId(Long typeId) {
        List<FirstPageBlog> byTypeId = blogMapper.getByTypeId(typeId);
        return byTypeId;
    }

    @Override
    public List<FirstPageBlog> getByTagId(Long tagId) {
        List<FirstPageBlog> byTagId = blogMapper.getByTagId(tagId);
        return byTagId;
    }

    @Override
    public List<Blog> getBlog(Integer number) {
        List<Blog> blog = blogMapper.getBlog(number);
        return blog;
    }

    @Override
    public List<Blog> getNewBlog() {
        List<Blog> newBlog = blogMapper.getNewBlog();
        return newBlog;
    }
}
